import { defineStore } from "pinia";
import {
    reqClassAll,
    reqClassList,
    reqClassAdd,
    reqClassById,
    reqClassUpdata,
    reqClassDelete
} from "@/api/class"
import type {
    classInfoData,
    classListFormData,
    classResponseData,
    classAllResponseData,
    classListResponseData
} from "@/api/class/type"
import { ClassState } from "./types/type";
let useClassStore = defineStore("Class", {
    state: (): ClassState => {
        return {
            classes: []
        }
    },
    actions: {
        // 总
        async getClassAll() {
            let result: classAllResponseData = await reqClassAll();
            if (result.status === 0) {
                this.classes = (result.data as classInfoData[])
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 部分
        async getClassList(data: classListFormData) {
            let result: classListResponseData = await reqClassList(data);
            if (result.status === 0) {
                this.classes = (result.data?.data as classInfoData[])
                return result.data
            } else {
                return Promise.reject(new Error(result.msg));
            }
        },
        // 添加
        async addClass(data: classInfoData) {
            let result: classResponseData = await reqClassAdd(data);
            if (result.status == 0) {
                return "OK"
            }
            else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // ID获取
        async getClassById(_id: string) {
            let result: classResponseData = await reqClassById(_id)
            if (result.status == 0) {
                return result.data
            } else {
                return Promise.reject(new Error(result.msg))

            }
        },
        // 修改
        async updateClass(data: classInfoData) {
            let result: classResponseData = await reqClassUpdata(data);
            if (result.status == 0) {
                return "OK"
            }
            else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 删除
        async deleteClass(classId: string) {
            let result: classResponseData = await reqClassDelete(classId);
            if (result.status == 0) {
                return "OK"
            }
            else {
                return Promise.reject(new Error(result.msg))
            }
        },
        getters: {

        }
    }

})
export default useClassStore